package com.example.zqq.shoushipassword;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.zqq.shoushipassword.view.NinePointLineView;

import java.util.ArrayList;
import java.util.List;

/**
 * 设置手势密码
 * GUE_PWD。IS_SET_FIRST、ERROR_POINT、SECOND_ERROR/
 */
public class MainActivity extends AppCompatActivity {

    private LinearLayout nine_con;  //九宫格容器
    private NinePointLineView nv;   //九宫格View

    private boolean isSetFirst = false; //判断是否是第一次输入手势密码
    private TextView showInfo;
    private TextView mBottom;
    private List<ImageView> mImageList = new ArrayList<>();
    private LinearLayout mBack;     //回退

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nv = new NinePointLineView(this, -1);
        findView();
    }

    @Override
    public void onBackPressed() {
        SharedPreferences shareData = getSharedPreferences("GUE_PWD", 0);
        shareData.edit().clear().apply();
        super.onBackPressed();
    }

    private void findView() {
        mBack = (LinearLayout) findViewById(R.id.back);
        nine_con = (LinearLayout) findViewById(R.id.nine_con);
        showInfo = (TextView) findViewById(R.id.show_set_info);
        mBottom = (TextView) findViewById(R.id.bottom);

        mImageList.add((ImageView) findViewById(R.id.image0));
        mImageList.add((ImageView) findViewById(R.id.image1));
        mImageList.add((ImageView) findViewById(R.id.image2));
        mImageList.add((ImageView) findViewById(R.id.image3));
        mImageList.add((ImageView) findViewById(R.id.image4));
        mImageList.add((ImageView) findViewById(R.id.image5));
        mImageList.add((ImageView) findViewById(R.id.image6));
        mImageList.add((ImageView) findViewById(R.id.image7));
        mImageList.add((ImageView) findViewById(R.id.image8));

        nine_con.addView(nv);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        getSetPwd();
    }

    /**
     * 绘制手势密码
     */
    private void getSetPwd() {
        final SharedPreferences shareDate = getSharedPreferences("GUE_PWD", 0);
        isSetFirst = shareDate.getBoolean("IS_SET_FIRST", false);                   //存储在下面
        boolean is_point_error = shareDate.getBoolean("ERROR_POINT", false);

        if (is_point_error) {
//            showInfo.setTextColor(getResources().getColor(R.color.guespwd));//提示字体颜色
            showInfo.setTextColor(Color.parseColor("#cb1a25"));//提示字体颜色
            showInfo.setText("至少选择4个点，请重新绘制");
        } else {
            if (!isSetFirst) {
                showInfo.setText("请绘制解锁图案");
                shareDate.edit().clear().apply();
            } else {
                init();

                showInfo.setText("请再次绘制解锁图案");
                mBottom.setText("重新设置手势密码");
                mBottom.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mImageList.clear();
                        shareDate.edit().putBoolean("IS_SET_FIRST", false).apply();
                        shareDate.edit().putBoolean("SECOND_ERROR", false).apply();
                        startActivity(new Intent(MainActivity.this, MainActivity.class));
                        finish();
                    }
                });
            }

            boolean is_second_error = shareDate.getBoolean("SECOND_ERROR", false);
            if (is_second_error) {
                showInfo.setTextColor(Color.parseColor("#cb1a25"));
                showInfo.setText("与上次不一致，请重新绘制");
            }
        }
    }


    //根据第一次输入的手势密码显示最上方的九个小点
    private void init() {
        SharedPreferences shareDate = getSharedPreferences("GUE_PWD", 0);
        String pwd = shareDate.getString("GUE_PWD", "NO HAVE PWD !");
        for (int i = 0; i < pwd.length(); i++) {
            mImageList.get(pwd.charAt(i) - '0').setImageResource(R.mipmap.h5_selected);
        }
    }

}
